from bqtool.utils.tools import get_ms_time, to_pdtime_ms
import time
from influxdb import InfluxDBClient, DataFrameClient
import pandas as pd
from pandasticsearch import DataFrame


def testtime():
    timeStr = '2018-12-19 10:01:00'
    ms = get_ms_time(timeStr)
    result = to_pdtime_ms(ms, tz=False)
    print(result)


def read_info():
    data_list = [{'measurement': 'win',
                  'tags': {'cpu': 'i7-7700HQ'},
                  'fields': {'cpu_info_user': 'my',
                             'cpu_info_system': 2,
                             'test': '小'
                             }},
                 {'measurement': 'win',
                  'tags': {'cpu': 'i7-7700HQ1'},
                  'fields': {'cpu_info_user': 'my',
                             'cpu_info_system': 2,
                             'test': '猫'
                             }}
                 ]
    return data_list


def pandasinsert():
    df = pd.DataFrame(data=list(range(30)), columns=['code'],
                      index=pd.date_range(start='2018-12-9',
                                          periods=30, freq='H'))
    df['name'] = '我'
    client = DataFrameClient('192.168.99.100', database='mydb')
    client.write_points(df, 'demo', protocol='json')

def get_data():
    df = DataFrame.from_es(url='http://10.2.111.171:24148', index='komsindex201812', doc_type='koms_car_gps').limit(10000)
    p_df = df.to_pandas()
    p_df.loc[:,'timestamp'] = to_pdtime_ms(p_df['timestamp'],tz=False)
    print('ok')


if __name__ == '__main__':
    # testtime()
    # client = DataFrameClient('10.2.111.65', database='mydb')  # 初始化
    # start = time.clock()
    # df = client.query("select * from mydb where time>='2017-05-16 05:00:00'  ")
    # df = pd.DataFrame(df['mydb'])
    # end = time.clock()
    # print(end - start)

    df = DataFrame.from_es(url='http://10.2.111.51:9200', index='ehlindex201809_10', doc_type='pass_car').limit(10000)
    start = time.clock()
    p_df = df.filter((df.timestamp >= 1539637200000) & (df.timestamp < 1539638400000)).to_pandas()
    end = time.clock()
    print(end - start)
    # get_data()
    print('ok')
